﻿
/*
bodystr:上传的内容
host:主机
port:端口
routepath:控制器的路径
*/
var xml2js = require('xml2js');
var encoding = require('encoding');
var request = require('request');
var fs = require('fs');

function PostData(bodystr, host, port, routepath,token,cb) {
    var http = require('http');
    var content = JSON.stringify(bodystr);
    var jsonheaders = {
        'Content-Type': 'application/json',
        'Content-Length': content.length,
        'token':token
    };

    var options = {
        hostname: host,//'127.0.0.1',
        port:port,// 80,
        path: routepath,//'/net/index.php?SerialPort/index/1000',
        method: 'POST',
        headers: jsonheaders
    };

    var req = http.request(options, function (res) {
        // console.log(res);
        res.setEncoding('utf8');
        // res.on('data', function (chunk) {
        //     console.log('BODY: ' + chunk);
        //     cb(res.statusCode,chunk);
        // });
        if (res.statusCode == 200) {  
            var body = "";  
            res.on('data', function (data) { body += data; })  
            res.on('end', function () { 
                // console.log(body);
                if (cb) {
                    cb(200,body); 
                }
            });  
        }  
        else {  
            if (cb) {
              cb(500,-1);
            }
        }  
    });

    req.on('error', function (e) {
        console.log('problem with request: ' + e.message);
    });

    req.write(content);
    req.end();
}
function PostData1(bodystr, host, port, routepath,cb) {
    var http = require('http');
    var content = JSON.stringify(bodystr);
    var jsonheaders = {
        'Content-Type': 'application/json;charset=utf-8',
        'Content-Length': content.length
    };

    var options = {
        hostname: host,//'127.0.0.1',
        port:port,// 80,
        path: routepath,//'/net/index.php?SerialPort/index/1000',
        method: 'POST',
        headers: jsonheaders
    };

    var req = http.request(options, function (res) {
        // console.log(res);
        res.setEncoding('utf8');
        // res.on('data', function (chunk) {
        //     console.log('BODY: ' + chunk);
        //     cb(res.statusCode,chunk);
        // });
        if (res.statusCode == 200) {  
            var body = "";  
            res.on('data', function (data) { body += data; })  
            res.on('end', function () { 
                // console.log(body);
                if (cb) {
                    cb(200,body); 
                }
                
            });  
        }  
        else {  
            if (cb) {
              cb(500,-1);
            }
           
        }  
    });

    req.on('error', function (e) {
        console.log('problem with request: ' + e.message);
    });

    req.write(content);
    req.end();
}
function PostData2(bodystr, url,cb) {
    var jsonheaders = {
        'Content-Type': 'application/json;charset=utf-8'
    };
    var options = {
        url: url,
        method: 'POST',
        json:true,
        body: bodystr,
        headers: jsonheaders
    };
    request(options, function(error, response, data) {
        // console.log(data);
        if (!error && response.statusCode == 200) {
            if (cb) {
                cb(200,data); 
            }
            
        }else{
            console.log(error);
        }
    });
}
// xml格式
function https_post(bodystr, host, port, routepath,cb) {
    var https = require('https');
    const content = Buffer.from(bodystr, 'utf-8');
    var jsonheaders = {
        "Content-Type":'application/xml;charset=utf-8',
        'Content-Length': content.length
    };

    var options = {
        hostname: host,//'127.0.0.1',
        port:port,// 443,
        path: routepath,//'/net/index.php?SerialPort/index/1000',
        method: 'POST',
        // data:'xml',
        headers: jsonheaders
    };
    // options.agent = new https.Agent(options);
    var req = https.request(options, function (res) {
        // console.log("222222");
        res.setEncoding('utf8');
        if (res.statusCode == 200) {  
            var body = "";  
            res.on('data', function (data) { body += data; })  
            res.on('end', function () { 
                // console.log(body);
                if (cb) {
                    cb(200,body); 
                }               
            });  
        }  
        else {  
            if (cb) {
                cb(500,-1);
            }            
        }  
    });

    req.on('error', function (e) {
        console.log('problem with request: ' + e.message);
    });

    req.write(content);
    req.end();
}
// json格式
function https_post2(bodystr, host, port, routepath,cb) {
    var xxxxx= 
     {
            "touser":"oxx5y1CZJ3Ew268Fjs1UJfprnV_c",
            "template_id":"GOOB-ygKu8vUF0CzuQPf7TzEyT4-kIEMzHU-Zt4_JA8",
            "url":"http://www.baidu.com",
            "data":
            {
                 "first":
                  {
                    "value":"inputdeviceid哈哈",
                    "color":"#173177"
                  },
                 "keyword1":
                 {
                    "value":"inputwarn_type",
                    "color":"#173177"
                 },
                 "keyword2":
                 {
                    "value":"2018-03-07 18:18:38",
                    "color":"#173177"
                 },
                 "remark":
                 {
                    "value":"d",
                    "color":"#173177"
                 }
            }
    }
    var https = require('https');
    var content = bodystr;
    // var content = bodystr;
    // var content = encoding.convert(bodystr,'UTF-8');
    content = JSON.stringify(content);
    // content = content.toString('utf8'); 
    // console.log('content',content);
    var jsonheaders = {
        "Content-Type":'application/json;charset=utf-8',
        'Content-Length':Buffer.byteLength(content, 'utf8') //; content.length
    };

    var options = {
        hostname: host,//'127.0.0.1',
        port:port,// 443,
        path: routepath,//'/net/index.php?SerialPort/index/1000',
        method: 'POST',
        // data:'xml',
        headers: jsonheaders
    };
    // options.agent = new https.Agent(options);
    var req = https.request(options, function (res) {
        res.setEncoding('utf8');
        if (res.statusCode == 200) {  
            var body = "";  
            res.on('data', function (data) { body += data; })  
            res.on('end', function () { 
                // console.log(body);
                if (cb) {
                    cb(200,body); 
                }
                
            });  
        }  
        else {  
            if (cb) {
                cb(500,-1);
            }
            
        }  
    });

    req.on('error', function (e) {
        console.log('problem with request: ' + e.message);
    });

    req.write(content);
    req.end();
}
// 携带支付证书,xml
function https_post3(bodystr, host, port, routepath,cb) {
    var https = require('https');
    const content = Buffer.from(bodystr, 'utf-8');
    var jsonheaders = {
        "Content-Type":'application/xml;charset=utf-8',
        'Content-Length': content.length
    };

    var options = {
        hostname: host,//'127.0.0.1',
        port:port,// 443,
        path: routepath,//'/net/index.php?SerialPort/index/1000',
        method: 'POST',
        // data:'xml',
        headers: jsonheaders,
        pfx:fs.readFileSync('./apiclient_cert.p12'),
        passphrase:'1489573162'
    };
    // options.agent = new https.Agent(options);
    var req = https.request(options, function (res) {
        // console.log("222222");
        res.setEncoding('utf8');
        if (res.statusCode == 200) {  
            var body = "";  
            res.on('data', function (data) { body += data; })  
            res.on('end', function () { 
                // console.log(body);
                if (cb) {
                    cb(200,body); 
                }               
            });  
        }  
        else {  
            if (cb) {
                cb(500,-1);
            }            
        }  
    });

    req.on('error', function (e) {
        console.log('problem with request: ' + e.message);
    });

    req.write(content);
    req.end();
}
exports.PostData = PostData;
exports.PostData1 = PostData1;
exports.PostData2 = PostData2;
exports.https_post = https_post;
exports.https_post2 = https_post2;
exports.https_post3 = https_post3;